package net.tngou.crawler.company;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;




import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Entities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**

 * Http请求  get与Post

 * @author 陈磊

 *

 */

public class HttpUtil {

	private  final Logger log= LoggerFactory.getLogger(HttpUtil.class);


	protected String userAgent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";	
	
	

	protected  int timeout=12000;	
	//17070498405 123456
//	private String cookie="zg_did=%7B%22did%22%3A%20%2216394d65e65d1-0f65abfdb08b748-4c312a7a-1fa400-16394d65e676b%22%7D; zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f=%7B%22sid%22%3A%201535088617977%2C%22updated%22%3A%201535088631575%2C%22info%22%3A%201535079226589%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.qichacha.com%22%2C%22cuid%22%3A%20%224799fdd24774a0d946ae7d55b82a4265%22%7D; UM_distinctid=16394d65e92383-0efd2d672be1f2-4c312a7a-1fa400-16394d65e931c7; CNZZDATA1254842228=1888929963-1527209850-https%253A%252F%252Fwww.baidu.com%252F%7C1535074209; Hm_lvt_3456bee468c83cc63fb5147f119f1075=1534726492,1534729540,1534925883,1535079226; _uab_collina=152721047467361360559916; _umdata=2BA477700510A7DF9597EE59F74883211E8BA175FB652A3DCC9B435A38E0C88DEC8602CD6DFB6A78CD43AD3E795C914C10A0DA414F62B442C7C225657AB4284E; PHPSESSID=2fgp6pusc5to0qstm3em989m47; acw_tc=AQAAAN4FRVB12AgAVU1xdhgQq6Un8i3w; Hm_lpvt_3456bee468c83cc63fb5147f119f1075=1535079232; hasShow=1";
	private String cookie="";
	
	//	private String cookie="";
	/**

	 * 取得页面信息

	* <p>Title: get<／p>

	* <p>Description: <／p>

	* @param url

	* @return

	 */
	public  Document get(String url) {
		Document doc = null;
		
		try {
			Connection con = Jsoup.connect(url);
			con.header("Cookie", cookie);
			doc = con.timeout(timeout).
					userAgent(userAgent).
					ignoreContentType(true).
					get();
//			Entities.EscapeMode.base.getMap().clear();
		} catch (IOException e) {

//			e.printStackTrace();
			log.error("取得HTTP请求{}出错！",url);
			return null;
		}
		return doc;
		
	}
	
	public  Document post(String url) {
		Document doc = null;
		
		try {
			Connection con = Jsoup.connect(url);
			con.header("Cookie", cookie);
			doc = con.timeout(timeout).
					userAgent(userAgent).
					ignoreContentType(true).
					post();
//			Entities.EscapeMode.base.getMap().clear();
		} catch (IOException e) {
//			e.printStackTrace();

			log.error("取得HTTP请求{}出错！",url);
			return null;
		}
		return doc;
		
	}
	
	/**

	 * 

	 * @param url   请求的URL

	 * @param data  参数

	 * @return

	 */
	public  Document post(String url,Map<String, String> data) {
		Document doc = null;
		try {
			
			
			Connection con = Jsoup.connect(url);
			con.header("Cookie", cookie);
			doc = con.data(data).
					ignoreContentType(true).
					post();
		} catch (IOException e) {
//			e.printStackTrace();

			log.error("取得HTTP请求{}出错！",url);
			return null;
		}
		return doc;
		
	}
	
	public  Document get(String url,Map<String, String> data) {
		Document doc = null;
		try {
			
			Connection con = Jsoup.connect(url);
			con.header("Cookie", cookie);
			doc = con.data(data).
					timeout(timeout).				
					userAgent(userAgent).
					ignoreContentType(true).
					get();
		} catch (IOException e) {
//			e.printStackTrace();

			log.error("取得HTTP请求{}出错！",url);
			return null;
		}
		return doc;
		
	}
	
	
	
	public  String getString(String url,Map<String, String> data) {
		String json="";
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	    Set<String> set = data.keySet();
	    
		set.forEach(e->{nvps.add(new BasicNameValuePair(e, data.get(e)));});
        try {
        	
        	
			URI uri = new URIBuilder(url).setParameters(nvps) .build();
        	// 创建httpget.    

            HttpGet httpget = new HttpGet(uri);  
            httpget.addHeader("User-Agent",userAgent);
            httpget.addHeader("Cookie", cookie);
		
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout( timeout).setConnectTimeout(timeout).build();//设置请求和传输超时时间      

            httpget.setConfig(requestConfig);
           // httpget.s

			CloseableHttpResponse response = httpclient.execute(httpget);
			// 获取响应实体    

            HttpEntity entity = response.getEntity();
     
            json=EntityUtils.toString(entity,"UTF-8");
            response.close(); 
		} catch (IOException | URISyntaxException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		} finally
		{
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block

				e.printStackTrace();
			}
		}
		
		return json;
		
	}

	public  String getString(String url,String cookie,Map<String, String> data) {
		String json="";
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	    Set<String> set = data.keySet();
	    
		set.forEach(e->{nvps.add(new BasicNameValuePair(e, data.get(e)));});
        try {
        	
        	
			URI uri = new URIBuilder(url).setParameters(nvps) .build();
        	// 创建httpget.    

            HttpGet httpget = new HttpGet(uri);  
            httpget.addHeader("User-Agent", userAgent);
            httpget.addHeader("Cookie", cookie);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout( timeout).setConnectTimeout(timeout).build();//设置请求和传输超时时间      

            httpget.setConfig(requestConfig);
           // httpget.s

			CloseableHttpResponse response = httpclient.execute(httpget);
			// 获取响应实体    

            HttpEntity entity = response.getEntity();
     
            json=EntityUtils.toString(entity,"UTF-8");
            response.close(); 
		} catch (IOException | URISyntaxException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		} finally
		{
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block

				e.printStackTrace();
			}
		}
		
		return json;
		
	}
	
   public  String postString(String url,Map<String, String> data) {
		
		
		String json=null;
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	    Set<String> set = data.keySet();
	    
		set.forEach(e->{nvps.add(new BasicNameValuePair(e, data.get(e)));});
        try {
        	
        	
			URI uri = new URIBuilder(url).setParameters(nvps) .build();
        	// 创建httpget.    

            HttpPost httppost = new HttpPost(uri);  
            httppost.setEntity(new UrlEncodedFormEntity(nvps));
            httppost.addHeader("User-Agent", userAgent);
            httppost.addHeader("Cookie", cookie);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout( timeout).setConnectTimeout(timeout).build();//设置请求和传输超时时间      

            httppost.setConfig(requestConfig);
           // httpget.s

			CloseableHttpResponse response = httpclient.execute(httppost);
			// 获取响应实体    

            HttpEntity entity = response.getEntity();
     
            json=EntityUtils.toString(entity,"UTF-8");
            response.close(); 
		} catch (IOException | URISyntaxException e) {
		
			e.printStackTrace();
			return null;
		} finally
		{
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return json;
		
	}
	
	public  String getString(String url) {
				
		String json=null;
		CloseableHttpClient httpclient = HttpClients.createDefault();  

        try {
        	 	
			URI uri = new URIBuilder(url).build();
        	// 创建httpget.    

            HttpGet httpget = new HttpGet(uri);  
            httpget.addHeader("User-Agent", userAgent);
            httpget.addHeader("Cookie", cookie);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout( timeout).setConnectTimeout(timeout).build();//设置请求和传输超时时间      

            httpget.setConfig(requestConfig);
           // httpget.s

			CloseableHttpResponse response = httpclient.execute(httpget);
			// 获取响应实体    

            HttpEntity entity = response.getEntity();
     
            json=EntityUtils.toString(entity,"UTF-8");
            response.close(); 
		} catch (IOException | URISyntaxException e) {
			
			log.error("页面请求错误:{}",url);
			e.printStackTrace();
		} finally
		{
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block

				e.printStackTrace();
			}
		}
		
		return json;
		
	}
	
}


